package com.ssyan.boot;

import com.ssyan.boot.entity.User;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.util.Arrays;
import java.util.List;

/**
 * @author ssyan
 * @Description 客户端
 * @create 2021/8/17 23:50
 * @Version 1.0
 */
public class Client {

    public static void main(String[] args) {
        //调用服务器地址
        WebClient webClient = WebClient.create("http://127.0.0.1:8082");

        //根据id查询
        String id = "1";
        User result = webClient.get()
                .uri("/users/{id}", id)
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToMono(User.class)
                .block();
        System.out.println(result);

        //查询所有
        Flux<User> allResult = webClient.get()
                .uri("/users")
                .accept(MediaType.APPLICATION_JSON)
                .retrieve()
                .bodyToFlux(User.class);
        allResult.map(person-> Arrays.asList(person)).buffer().doOnNext(System.out::println).blockFirst();
        //allResult.buffer().toStream().forEach(System.out::println);
    }
}
